package com.neu.his.controller.lastVerssion;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.neu.his.core.Result;
import com.neu.his.entity.*;
import com.neu.his.entity.request.lastVerssion.AddOneDParams;
import com.neu.his.entity.request.lastVerssion.AddOneNDParams;
import com.neu.his.entity.request.lastVerssion.GetDDParams;
import com.neu.his.mapper.DrugDisturbMapper;
import com.neu.his.mapper.DrugMapper;
import com.neu.his.mapper.NonDrugDisturbMapper;
import com.neu.his.mapper.NonDrugMapper;
import com.neu.his.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Description: neu-his
 * <p>
 * Created by 31316 on 2020/7/19 16:56
 */
@RestController
@RequestMapping("/checkApplication")
public class CheckApplicationController {
    @Autowired
    DrugMapper drugMapper;

    @Autowired
    NonDrugMapper nonDrugMapper;

    @Autowired
    NonDrugDisturbMapper nonDrugDisturbMapper;

    @Autowired
    DrugDisturbMapper drugDisturbMapper;

    @GetMapping("/getNonDrug")
    public Result getNonDrug() {
        List<NonDrug> list = nonDrugMapper.selectList(new QueryWrapper<>());
        return Result.success(list);
    }

    @GetMapping("/getDrug")
    public Result getDrug() {
        List<Drug> list = drugMapper.selectList(new QueryWrapper<>());
        return Result.success(list);
    }

    @PostMapping("/addOneND")
    public Result addOneND(@RequestBody AddOneNDParams addOneNDParams) {
        if (nonDrugDisturbMapper.selectOne(new QueryWrapper<NonDrugDisturb>().ne("remove_mark", 1L)
                .eq("non_drug_id", addOneNDParams.getNonDrugId()).eq("diag_id", addOneNDParams.getDiagId())) != null)
            return Result.fail("已存在");
        NonDrugDisturb nonDrugDisturb = new NonDrugDisturb();
        nonDrugDisturb.setNonDrugId(addOneNDParams.getNonDrugId());
        if (addOneNDParams.getNonDrugNum() != null) nonDrugDisturb.setNum(addOneNDParams.getNonDrugNum().intValue());
        else nonDrugDisturb.setNum(1);
        nonDrugDisturb.setDiagId(addOneNDParams.getDiagId());
        nonDrugDisturb.setCreateDate();
        nonDrugDisturb.setHaveDeliver(false);
        nonDrugDisturb.setPaid(false);
        nonDrugDisturb.setNonDrugResult("");
        nonDrugDisturb.setTotalCount(nonDrugMapper.selectOne(new QueryWrapper<NonDrug>()
                .ne("remove_mark", 1L).eq("id", nonDrugDisturb.getNonDrugId()))
                .getNonDrugPrice().multiply(new BigDecimal(nonDrugDisturb.getNum())));
        nonDrugDisturbMapper.insert(nonDrugDisturb);
        return Result.success("插入成功");
    }

    @PostMapping("/addOneD")
    public Result addOneD(@RequestBody AddOneDParams addOneDParams) {
        if (drugDisturbMapper.selectOne(new QueryWrapper<DrugDisturb>().ne("remove_mark", 1L)
                .eq("drug_id", addOneDParams.getDrugId()).eq("diag_id", addOneDParams.getDiagId())) != null)
            return Result.fail("已存在");
        DrugDisturb DrugDisturb = new DrugDisturb();
        DrugDisturb.setDrugId(addOneDParams.getDrugId());
        if (addOneDParams.getDrugNum() != null) DrugDisturb.setNum(addOneDParams.getDrugNum().intValue());
        else DrugDisturb.setNum(1);
        DrugDisturb.setDiagId(addOneDParams.getDiagId());
        DrugDisturb.setCreateDate();
        DrugDisturb.setHaveDeliver(false);
        DrugDisturb.setTotalCount(drugMapper.selectOne(new QueryWrapper<Drug>()
                .ne("remove_mark", 1L).eq("id", DrugDisturb.getDrugId()))
                .getDrugPrice().multiply(new BigDecimal(DrugDisturb.getNum())));
        drugDisturbMapper.insert(DrugDisturb);
        return Result.success("插入成功");
    }

    @PostMapping("/getDrugDistribute")
    public Result getDrugDistribute(@RequestBody GetDDParams getDDParams){
        List<DrugDisturb> result= drugDisturbMapper.selectList(new QueryWrapper<DrugDisturb>()
                .ne("remove_mark", 1L).eq("diag_id",getDDParams.getDiagId()));
        PageList<DrugDisturb> pageList=new PageList(result,getDDParams.getCurrentPage(),getDDParams.getPageSize());
        return Result.success(pageList);
    }

    @PostMapping("/getNonDrugDistribute")
    public Result getNonDrugDistribute(@RequestBody GetDDParams getDDParams){
        List<NonDrugDisturb> result= nonDrugDisturbMapper.selectList(new QueryWrapper<NonDrugDisturb>()
                .ne("remove_mark", 1L).eq("diag_id",getDDParams.getDiagId()));
        PageList<DrugDisturb> pageList=new PageList(result,getDDParams.getCurrentPage(),getDDParams.getPageSize());
        return Result.success(pageList);
    }

    @PostMapping("/updateDD")
    public Result updateDD(@RequestBody DrugDisturb drugDisturb){
        drugDisturb.countTotal();
        drugDisturbMapper.updateById(drugDisturb);
        return Result.success("修改成功");
    }

    @PostMapping("/updateNDD")
    public Result updateNDD(@RequestBody NonDrugDisturb nonDrugDisturb){
        nonDrugDisturb.countTotal();
        nonDrugDisturbMapper.updateById(nonDrugDisturb);
        return Result.success("修改成功");
    }
}
